import pymysql


class DBTools(object):
    @classmethod
    def __create_conn(self):
        conn = pymysql.connect(host="127.0.0.1", port=3306, user="root", passwd="123456", db="ihrm")
        return conn

    @classmethod
    def query_one(cls, sql):
        my_coon = None
        my_course = None
        res = None
        try:
            my_coon = DBTools.__create_conn()
            my_course = my_coon.cursor()
            my_course.execute(sql)
            res = my_course.fetchone()
        except Exception as e:
            print(e)
        finally:
            my_course.close()
            my_coon.close()
            return res
    @classmethod
    def db_uid(cls,sql):
        my_conn = None
        my_course = None
        try:
            my_conn = DBTools.__create_conn()
            my_course = my_conn.cursor()
            my_course.execute(sql)
            my_conn.commit()
        except Exception as e:
            print(e)
            my_conn.rollback()
        finally:
            my_course.close()
            my_conn.close()
if __name__ == '__main__':
    result = DBTools.query_one("select * from books")
    print(result)